//
//  ControlMetrics.m
//  iBasic
//
//  Created by Andrew Barry on 26/04/08.
//  Copyright 2008 Barry Computer Services. All rights reserved.
//

#import "ControlMetrics.h"


@implementation ControlMetrics

+(NSDictionary *) getControlAlignment: (NSView *)view forFrame: (NSRect) frame {
//	NSRect frame = [view frame];

	if ([view isKindOfClass:[NSButton class]]) {
		NSFont *f = [(NSButton *) view font];
		if (f != nil) {
			
			NSLayoutManager *layout = [[NSLayoutManager alloc] init];
			[layout setUsesScreenFonts:YES];
			[layout setTypesetterBehavior: 0];
			[layout autorelease];
			float baseline = [layout defaultBaselineOffsetForFont: f];
			float h = [layout defaultLineHeightForFont:f];
			
			float dy = (int) ((24 - h) / 2);

			int mid = frame.origin.y + frame.size.height / 2;
			
			return [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:mid + 8], @"bottom", [NSNumber numberWithInt:mid - 12], @"top",
					 [NSNumber numberWithInt:mid + dy - 15 + baseline], @"baseline",
					[NSNumber numberWithInt:frame.origin.x], @"left", [NSNumber numberWithInt:frame.origin.x + frame.size.width - 1], @"right", 
					 nil];
		}
	} else if ([view isKindOfClass:[NSTextField class]]) {
		NSTextField *tf = (NSTextField *) view;
		NSFont *f = [(NSTextField *) view font];
		
		NSLayoutManager *layout = [[NSLayoutManager alloc] init];
		[layout setUsesScreenFonts:YES];
		[layout setTypesetterBehavior: NSTypesetterBehavior_10_2_WithCompatibility];
		[layout autorelease];
		float baseline = [layout defaultBaselineOffsetForFont: f];
		int border = 0;
		if ([tf isBezeled]) {
			border += 3;
			return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:frame.origin.y], @"top", [NSNumber numberWithInt:frame.origin.y + frame.size.height - 1], @"bottom",
					[NSNumber numberWithInt:frame.origin.y + baseline - 1 + border], @"baseline",
					[NSNumber numberWithInt:frame.origin.x], @"left", [NSNumber numberWithInt:frame.origin.x + frame.size.width - 1], @"right", 
					nil];
		} else {
			return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:frame.origin.y + baseline - 1 + border], @"baseline",
					[NSNumber numberWithInt:frame.origin.x], @"left", 
					nil];
		}
		
	}
	return [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:frame.origin.y], @"top", [NSNumber numberWithInt:frame.origin.y + frame.size.height - 1], @"bottom", 
														[NSNumber numberWithInt:frame.origin.x], @"left", [NSNumber numberWithInt:frame.origin.x + frame.size.width - 1], @"right", 
													nil];
}

@end
